package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService  consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService IConsumptionItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/save")
    @RequiredPermission(name="根据预约生成结算单",expression = "consumption:save")
    public String save(Long appointmentId){
        Consumption consumption = consumptionService.save(appointmentId);
        return "redirect:/consumption/input?id="+consumption.getId();
    }


    @RequestMapping("/list")
    @RequiredPermission(name="结算单查询",expression = "consumption:list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo){
        model.addAttribute("businesses",businessService.listAll());
        model.addAttribute("consumptionStatusEnums", ConsumptionStatusEnum.values());
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "consumption/list";


//                List<Consumption> consumptions = consumptionService.listAll();
//        model.addAttribute("consumptions",consumptions);
//        return "consumption/list";  //找视图 / WEB-INF/views/consumption/list.jsp
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="结算单删除",expression = "consumption:delete")
    public String delete(Long id){
        if (id!=null) {
            consumptionService.delete(id);
        }
        return "redirect:/consumption/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/input")
    @RequiredPermission(name="结算单新增或修改",expression = "consumption:input")
    public String input(Long id,Model model){
        model.addAttribute("businesses",businessService.listAll());
        model.addAttribute("categories",systemDictionaryService.queryBySn("business"));
        model.addAttribute("payTypes",systemDictionaryService.queryBySn("pay_type"));

        if (id!=null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);
            List<ConsumptionItem> consumptionItems = IConsumptionItemService.queryByCno(consumption.getCno());
            model.addAttribute("consumptionItems",consumptionItems);

            BigDecimal totalAmount = BigDecimal.ZERO;
            BigDecimal totalDiscountAmount = BigDecimal.ZERO;
            for (ConsumptionItem consumptionItem : consumptionItems) {
                BigDecimal amount = consumptionItem.getAmount();
                totalAmount  = totalAmount.add(amount);
                BigDecimal discountAmount = consumptionItem.getDiscountAmount();
                totalDiscountAmount = totalDiscountAmount.add(discountAmount);
            }
            model.addAttribute("totalAmount",totalAmount);
            model.addAttribute("totalDiscountAmount",totalDiscountAmount);



        }
        return "consumption/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="结算单新增或修改",expression = "consumption:saveOrUpdate")
    public String saveOrUpdate(Consumption consumption){
        if (consumption.getId()!=null) {
            consumptionService.update(consumption);
        }else {
          consumptionService.save(consumption);
        }
        return "redirect:/consumption/list";
    }
}
